/**
 * task.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "task.h"
#include "constraints/taskconstraint.h"
#include "../utils/visitor/visitor.h"
#include <sstream>
#include <boost/foreach.hpp>

///////////////////////////////////////////////////////////////
Task::Task(const std::string & a_taskName ) :
    Visitable(),
    Printable(),
    m_taskName( a_taskName ),
    m_constraints()
{
}

///////////////////////////////////////////////////////////////
Task::~Task()
{
    ClearConstraints();
    m_taskName = UNNAMED;
}

///////////////////////////////////////////////////////////////
void Task::ClearConstraints()
{
    BOOST_FOREACH( TaskConstraint * constraint, m_constraints )
    {
        if( constraint )
            delete constraint;
    }

    m_constraints.clear();
}

///////////////////////////////////////////////////////////////
void Task::Accept( Visitor * a_visitor , bool a_depthFirst )
{
    if( a_visitor )
    {
        if( !a_depthFirst )
            a_visitor->Visit( this );

        VisitConstraints( a_visitor );

        if( a_depthFirst )
            a_visitor->Visit( this );
    }
}

///////////////////////////////////////////////////////////////
void Task::VisitConstraints( Visitor * a_visitor, bool a_depthFirst )
{
    BOOST_FOREACH( TaskConstraint * constraint, m_constraints )
    {
        if( constraint )
            constraint->Accept( a_visitor, a_depthFirst );
    }
}

///////////////////////////////////////////////////////////////
std::string Task::Print() const
{
    std::ostringstream strStream;
    strStream << "Task : " << m_taskName << std::endl;
    strStream << PrintConstraints();

    return strStream.str();
}

///////////////////////////////////////////////////////////////
std::string Task::PrintConstraints() const
{
    std::ostringstream strStream;
    strStream << "Constraints : ";

    BOOST_FOREACH( TaskConstraint * constraint, m_constraints )
    {
        if( constraint )
             strStream << std::endl << constraint->Print();
    }

    return strStream.str();
}

///////////////////////////////////////////////////////////////
void Task::TaskName( const std::string & a_taskName )
{
    m_taskName = a_taskName;
}

///////////////////////////////////////////////////////////////
const std::string & Task::TaskName() const
{
    return m_taskName;
}

///////////////////////////////////////////////////////////////
void Task::Add( TaskConstraint * a_constraint )
{
    if( a_constraint )
        m_constraints.insert( a_constraint );
}

///////////////////////////////////////////////////////////////
std::size_t Task::NConstraints() const
{
    return m_constraints.size();
}
